package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.convert.CarConvert;
import com.travel.dto.CarOrderDto;
import com.travel.dto.LookForCarDto;
import com.travel.dto.RentCarOrderDto;
import com.travel.feign.CarOrderFeign;
import com.travel.mapper.CarRentalCompanyMapper;
import com.travel.mapper.CarsMapper;
import com.travel.mapper.ImagesMapper;
import com.travel.pojo.CarPackage;
import com.travel.pojo.CarRentalCompany;
import com.travel.pojo.Cars;
import com.travel.service.CarPackageService;
import com.travel.mapper.CarPackageMapper;
import com.travel.vo.LookForCarVo;
import com.travel.vo.RentalDetailsVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author rjq51
 * @description 针对表【car_package】的数据库操作Service实现
 * @createDate 2023-05-11 10:10:54
 */
@Service
public class CarPackageServiceImpl extends ServiceImpl<CarPackageMapper, CarPackage>
        implements CarPackageService {

    @Resource
    private CarPackageMapper carPackageMapper;

    @Resource
    private CarRentalCompanyMapper carRentalCompanyMapper;

    @Resource
    private CarsMapper carsMapper;

    @Resource
    private ImagesMapper imagesMapper;

    @Resource
    private CarOrderFeign carOrderFeign;

    /**
     * 查询车辆相关
     * @param carPackage
     * @return
     */
//    @Override
//    public List<CarPackage> selectCarPackaage(CarPackage carPackage) {
//        return null;
//    }

    /**
     * 根据车辆所在城市查询车辆
     *
     * @param cityid
     * @return
     */
    @Override
    public List<CarPackage> SearchVehicleLocation(Integer cityid) {
        QueryWrapper<CarPackage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_id", cityid);
        return carPackageMapper.selectList(queryWrapper);
    }


    /**
     * 根据租车公司id查询汽车套餐
     *
     * @param carCompanyId
     * @return
     */
    @Override
    public List<CarPackage> CarRentalCompany(Integer carCompanyId) {
        QueryWrapper<CarPackage> carPackageQueryWrapper = new QueryWrapper<>();
        carPackageQueryWrapper.eq("car_company_id", carCompanyId);
        return carPackageMapper.selectList(carPackageQueryWrapper);
    }

    /**
     * 根据价格区间筛选车辆套餐
     *
     * @param
     * @return
     */
    @Override
    public List<CarPackage> PriceScreening(@Param("PriceState") Integer PriceState) {
        List<CarPackage> packageList = carPackageMapper.PriceScreening(PriceState);
        return packageList;
    }

    /**
     * 根据条件删选车辆
     *
     * @param lookForCarDto
     * @return
     */
    @Override
    public List<LookForCarVo> MultiConditionScreening(LookForCarDto lookForCarDto) {
        List<LookForCarVo> lookForCarVos = carPackageMapper.MultiConditionScreening(lookForCarDto);
        return lookForCarVos.stream().peek(o -> {
            String carIdString = "car" + o.getId();
            //查询出图片
            List<String> stringList = imagesMapper.selectImagesByForeignId(carIdString);
            //设置图片
            o.setImages(stringList);
        }).collect(Collectors.toList());
    }

    /**
     * 根据汽车id查询出租车公司信息
     *
     * @param carId
     * @return
     */
    @Override
    public List<CarRentalCompany> AToCarSearchCompany(Integer carId) {
        //查询出传入的汽车id对应的套餐
        QueryWrapper<CarPackage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", carId);
        List<CarPackage> packageList = carPackageMapper.selectList(queryWrapper);
        //创建一个汽车公司套餐
        List<CarRentalCompany> companyList = new ArrayList<>();
        //利用stream 循环每个packageList 拿到packageList 中租车公司id 和租车公司表中一致的数据
        packageList.stream().forEach(o -> {
            CarRentalCompany rentalCompany = carRentalCompanyMapper.selectById(o.getCarCompanyId());
            if (rentalCompany != null) {
                companyList.add(rentalCompany);
            }
        });
        //返回租车公司信息
        return companyList;
    }

    /**
     * 根据汽车id以及公司id查询出数据
     *
     * @param carId
     * @param carCompanyId
     * @return
     */
    @Override
    public RentalDetailsVo RentalDetails(Integer carId, Integer carCompanyId) {
        //根据传回的汽车id和公司id查询出套餐
        QueryWrapper<CarPackage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", carId).eq("car_company_id", carCompanyId);

        List<CarPackage> packageList = carPackageMapper.selectList(queryWrapper);
        //抽出packageList 中的car汽车信息 以及 公司信息
        List<RentalDetailsVo> rentalDetailsVos = packageList.stream().map(o -> {
            //查询出当前id对应的汽车 和租车公司
            Cars cars = carsMapper.selectById(o.getCarId());
            String foreignid = "car" + cars.getId();//拿到汽车图片id
            List<String> stringList = imagesMapper.selectImagesByForeignId(foreignid);
            CarRentalCompany carRentalCompany = carRentalCompanyMapper.selectById(o.getCarCompanyId());
            //如果都不为空 就将内容转入vo中 否则返回null
            if (cars != null && carRentalCompany != null) {
                RentalDetailsVo rentalDetailsVo = CarConvert.INSTANCE.CarRentalDetailsVo(carRentalCompany, cars);
                rentalDetailsVo.setImages(stringList); //将图片塞进去
                rentalDetailsVo.setPrice(o.getPrice());
                return rentalDetailsVo;
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList()); //filter(Objects::nonNull) 过滤流中的元素 只保留非空的

        //如果vo不为空就返回第一个值  否则就返回null
        if (!rentalDetailsVos.isEmpty()) {
            return rentalDetailsVos.get(0);
        } else {
            return null;
        }
    }

    /**
     * 查询所有汽车套餐  以及图片
     *
     * @return
     */
    @Override
    public List<LookForCarVo> GetlistcarPackage() {
        QueryWrapper<CarPackage> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("number_of_cars", 1);
        List<CarPackage> packageList = carPackageMapper.selectList(null);

        if (!packageList.isEmpty()) { //如果不为空
            List<LookForCarVo> lookForCarVo = new ArrayList<>();
            //循环将查询的套餐对象放到vo中
            for (CarPackage carPackage : packageList) {
                //拼接汽车id， 拿到汽车id在图片表中查询对应的图片
                Integer carId = carPackage.getCarId();
                //拿到单个车对象
                Cars cars = carsMapper.selectById(carId);
                LookForCarVo carVo = CarConvert.INSTANCE.LOOKcars(cars);
                String car = "car";
                String carIdString = car + carId;
                //查询出图片
                List<String> stringList = imagesMapper.selectImagesByForeignId(carIdString);
                carVo.setPrice(carPackage.getPrice());
                carVo.setPhotograph(carPackage.getPhotograph());
                carVo.setId(carPackage.getId());
                carVo.setCityId(carPackage.getCityId());
                carVo.setCarid(carId);
                //转换对象 再将图片放进去
                carVo.setImages(stringList);
                lookForCarVo.add(carVo);
            }
            return lookForCarVo;
        }
        return null;
    }

    /**
     * 生成订单
     *
     * @param carOrderDto
     * @return
     */
    @Override
    public HashMap<String, String> AutomobileOrder(CarOrderDto carOrderDto) {
        //查询出租车公司
        CarRentalCompany carRentalCompany = carRentalCompanyMapper.selectById(carOrderDto.getCarCompanyId());
        Cars cars = carsMapper.selectById(carOrderDto.getCarId());
        carOrderDto.setCompany(carRentalCompany.getName());
        carOrderDto.setTotalprice(carOrderDto.getPrice().multiply(BigDecimal.valueOf(carOrderDto.getRentalDays())));

        RentCarOrderDto rentCarOrderDto = new RentCarOrderDto();
        rentCarOrderDto.setOrderId(carOrderDto.getOrderNumber());
        rentCarOrderDto.setCityId(carRentalCompany.getCityId());
        rentCarOrderDto.setIdNumber(carOrderDto.getIdCard());
        rentCarOrderDto.setGuestName(carOrderDto.getDriver());
        rentCarOrderDto.setPhonenumber(carOrderDto.getPhone());
        rentCarOrderDto.setCarCompanyName(carOrderDto.getCarCompanyId());
        rentCarOrderDto.setCarName(carOrderDto.getCarName());
        rentCarOrderDto.setCarType(carOrderDto.getCarType());
        rentCarOrderDto.setDoors(String.valueOf(carOrderDto.getDoors()));
        rentCarOrderDto.setDrivingStyle(carOrderDto.getDrivingStyle());
        rentCarOrderDto.setEngineCapacity(String.valueOf(cars.getEngineCapacity()));
        rentCarOrderDto.setTotalPrice(carOrderDto.getTotalprice());
        //通过远程调用 订单
        HashMap<String, String> stringHashMap = carOrderFeign.generateOrder(rentCarOrderDto);
        return stringHashMap;
    }
}




